using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using SCSEngine.Mathematics;
using SCSEngine.ResourceManagement;
using SCSEngine.ScreenManagement;
using SCSEngine.ScreenManagement.Implement;
using SCSEngine.Services;
using SCSEngine.Services.Input;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using TextEffect;
using SVLvsXNA;
using SVLvsXNA.TriUanGame;
using Microsoft.Xna.Framework.Input;

namespace TriUan.TriUanGame.GameScreen
{
    public enum TextIndex
    { 
        Help,
    }

    public class HelpScreen : BaseGameScreen
    {
        private SCSServices services;
        private SpriteBatch spriteBatch;
        private InputHandle inputHandle;
        private IResourceManager resourceManager;
        private Texture2D background;
        private Texture2D[] shape;//One, shape[1], shape[2], shape[3], shape[4], shape[5], shape[6];
        private Vector2[] resultPosition,beginPosition, currentPosition;//, resultPosition[1], resultPosition[2], resultPosition[3], resultPosition[4], resultPosition[5], resultPosition[6];
        private Texture2D buttonPlay;
        private Rectangle recButtonPlay;
        private Texture2D finger;
        private Texture2D result;
        private Vector2 velocity;
        private float moveAlpha;
        private int index;
        private bool isBegin, isShowTutImgDone;
        private String textHelp;
        private SpriteFont font;
        private SpriteFont fontTitle;
        String parsedText;
        private CTextEffect textEffect;
        Rectangle textBox;

        public HelpScreen(IGameScreenManager manager)
            :base(manager)
        {
            services = (SCSServices)manager.GamePage.Services.GetService(typeof(SCSServices));
            spriteBatch = services.SpriteBatch;
            inputHandle = services.InputHandle;
            TouchPanel.EnabledGestures = GestureType.Tap;
            resourceManager = (IResourceManager)manager.GamePage.Services.GetService(typeof(IResourceManager));
            this.Init();
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Draw(background, Vector2.Zero, Color.White);
            textEffect.DrawStringHighlight(fontTitle, GameInformation.GetTitle(InformationIndex.TitleHelp), new Vector2(450,0),Color.Red,Color.Black,2);
            spriteBatch.Draw(result, new Vector2(50,50), new Rectangle(0, 0, (int)result.Width, (int)result.Height), Color.White, 0f, Vector2.Zero, 0.8f, SpriteEffects.None, 0f);
            spriteBatch.Draw(shape[0], currentPosition[0], new Rectangle(0, 0, (int)shape[0].Width, (int)shape[0].Height), Color.White, 0f, Vector2.Zero, 0.8f,SpriteEffects.None,0f);
            spriteBatch.Draw(shape[1], currentPosition[1], new Rectangle(0, 0, (int)shape[1].Width, (int)shape[1].Height), Color.White, 0f, Vector2.Zero, 0.8f, SpriteEffects.None, 0f);
            spriteBatch.Draw(shape[2], currentPosition[2], new Rectangle(0, 0, (int)shape[2].Width, (int)shape[2].Height), Color.White, 0f, Vector2.Zero, 0.8f, SpriteEffects.FlipVertically, 0f);
            spriteBatch.Draw(shape[3], currentPosition[3], new Rectangle(0, 0, (int)shape[3].Width, (int)shape[3].Height), Color.White, MathHelper.ToRadians(45.0f), Vector2.Zero, 0.8f, SpriteEffects.FlipVertically, 0f);
            spriteBatch.Draw(shape[4], currentPosition[4], new Rectangle(0, 0, (int)shape[4].Width, (int)shape[4].Height), Color.White, MathHelper.ToRadians(-135.0f), Vector2.Zero, 0.8f, SpriteEffects.None, 0f);
            spriteBatch.Draw(shape[5], currentPosition[5], new Rectangle(0, 0, (int)shape[5].Width, (int)shape[5].Height), Color.White, 0f, Vector2.Zero, 0.8f, SpriteEffects.FlipVertically, 0f);
            spriteBatch.Draw(shape[6], currentPosition[6], new Rectangle(0, 0, (int)shape[6].Width, (int)shape[6].Height), Color.White, MathHelper.ToRadians(-90.0f), Vector2.Zero, 0.8f, SpriteEffects.None, 0f);
            spriteBatch.Draw(buttonPlay, new Vector2(recButtonPlay.X,recButtonPlay.Y), Color.White);
            if(isBegin)
                spriteBatch.Draw(finger, currentPosition[index], Color.White);
            if (isShowTutImgDone)
            {
                textEffect.DrawAlphaString(textEffect.TypeWriterText, textEffect.TextPosition);
            }
            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            if (this.GamePage.IsBackKeyPress)
            {
                Manager.RemoveCurrent();

                this.GamePage.IsBackKeyPress = false;
            }

            HandleInput(inputHandle.Gestures);
            if (isShowTutImgDone)
            {
                textEffect.UpdateTypeWriter(gameTime);
            }
            if (isBegin)
            {
                BeginHelpTutorial(gameTime);
            }

            base.Update(gameTime);
        }

        public void HandleInput(List<GestureSample> gestures)
        {
            foreach (GestureSample gestureSample in gestures)
            {
                if (gestureSample.GestureType == GestureType.Tap)
                {
                    if (recButtonPlay.Contains(new Point((int)gestureSample.Position.X, (int)gestureSample.Position.Y)))
                    {
                        isBegin = true;
                        isShowTutImgDone = false;
                        if (currentPosition[0].X == resultPosition[0].X)
                            InitBeginPosition();
                        index = 0;
                        textEffect.InitTypeWriter(parsedText, 50);
                    }
                }
            }
        }
        public void Init()
        {
            velocity = new Vector2(-1f, -1f);
            beginPosition = new Vector2[7];
            resultPosition = new Vector2[7];
            currentPosition = new Vector2[7];
            shape = new Texture2D[7];
            moveAlpha = 0.0005f;
            InitBeginPosition();

            resultPosition[0] = new Vector2(172, 51);
            resultPosition[1] = new Vector2(93, 130);
            resultPosition[2] = new Vector2(172, 162);
            resultPosition[3] = new Vector2(212, 363);
            resultPosition[4] = new Vector2(196, 379);
            resultPosition[5] = new Vector2(211, 283);
            resultPosition[6] = new Vector2(51, 130);

            this.LoadContent();

            recButtonPlay = new Rectangle(600, 400, buttonPlay.Width, buttonPlay.Height);


            textBox = new Rectangle(360, 100, 370, 335);
            textEffect = new CTextEffect(spriteBatch, font, new Vector2(textBox.X, textBox.Y), textBox);
            textHelp = GameInformation.GetHelp();
            parsedText = textEffect.CreateWrapText(textHelp);
            textEffect.InitTypeWriter(parsedText, 50);
        }

        public void InitBeginPosition()
        {
            for (int i = 0; i < 7; i++)
            {
                beginPosition[i] = new Vector2(GRandom.RandomFloat(400, 550),GRandom.RandomFloat(150,300));
                currentPosition[i] = beginPosition[i];
            }
        }

        public void LoadContent()
        {
            background = resourceManager.GetResource<Texture2D>("MainBackground");
            result = resourceManager.GetResource<Texture2D>("ResultHelp");
            finger = resourceManager.GetResource<Texture2D>("Finger");
            buttonPlay = resourceManager.GetResource<Texture2D>(Language.Prep + "play");
            font = resourceManager.GetResource<SpriteFont>("InformationFont");
            fontTitle = resourceManager.GetResource<SpriteFont>("TitleFont");
            shape[0] = resourceManager.GetResource<Texture2D>("1");
            shape[1] = resourceManager.GetResource<Texture2D>("2");
            shape[2] = resourceManager.GetResource<Texture2D>("3");
            shape[3] = resourceManager.GetResource<Texture2D>("4");
            shape[4] = resourceManager.GetResource<Texture2D>("5");
            shape[5] = resourceManager.GetResource<Texture2D>("6");
            shape[6] = resourceManager.GetResource<Texture2D>("7");
        }

        public void BeginHelpTutorial(GameTime gameTime)
        {
            
            if (!isBegin)
                return;
            {
                velocity.X +=velocity.X * gameTime.ElapsedGameTime.Milliseconds * moveAlpha;
                currentPosition[index].X += velocity.X;
                currentPosition[index].Y = GetSingleCoordinateFromTwoPoint(resultPosition[index], beginPosition[index], currentPosition[index].X);
                if (currentPosition[index].X <= resultPosition[index].X)
                {
                    currentPosition[index] = resultPosition[index];
                    velocity = new Vector2(-1f, -1f);
                    index++;
                }
            }
            if (currentPosition[6] == resultPosition[6])
            {
                isBegin = false;
                index = 0;
                isShowTutImgDone = true;
            }
        }

        public float GetSingleCoordinateFromTwoPoint(Vector2 A, Vector2 B, float x)
        {
            Vector2 VTPT = new Vector2(B.Y - A.Y, A.X - B.X);
            float ratio = -(VTPT.X * A.X + VTPT.Y * A.Y);
            return (-ratio - x*VTPT.X)/(float)VTPT.Y;
        }
    }
}
